package com.itheima.dubbo.service;

import com.itheima.dubbo.User;
import com.itheima.dubbo.UserFace;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Description:
 * @Version: V1.0
 */
//@DubboService
// 0：不重试 默认是2，重试2次，不包含第一次的调用
// 冪等性：无论发送多少次相同的请求，结果是一致的！
// GET  DELETE  PUT（表单发起修改 冪等）  POST（非冪等）
// 结论：dubbo的远程调用过程中，对于非冪等操作，建议设置重试次数0，冪等性操作也建议重试次数不要过多，最好默认
//@DubboService(retries = 0)
//@DubboService(timeout = 2000)
// 方法级别
//@DubboService(methods = {
//        @Method(name = "findById",timeout = 10, retries = 0),
//        @Method(name = "hello",timeout = 10000),
//})
//@DubboService(version = "${dubbo.application.version}")
@DubboService( weight = 200) // 设置权重
public class UserFaceImpl implements UserFace {

    @Override
    public String hello(String username) {
        return "Hello Dubbo username：" + username;
    }


    @Value("${server.port}")
    private Integer port;

    @Override
    public User findById(User user) {

        System.out.println("findById ...." + port);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //RpcContext context = RpcContext.getContext();
        //String it = context.getAttachment("it");

        String it = RpcContext.getContext().getAttachment("it");
        System.out.println(it);
        return new User(user.getId(),user.getName()+"-itheima");
    }
}
